package com.ltv.xiangqiplugin.views;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class HtmlView extends JFrame implements ActionListener {    
    JPanel mainPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JLabel descrLabel = new JLabel("<html>You can embed this game to your web page by copy-past below HTML to your web page<br>" +
    		"Or you can add this game to your comment by copy-paste below HTML to your comment</html>");
  
    JTextArea textArea = new JTextArea();
    final JPopupMenu cutpasteMenu = new JPopupMenu();
    JMenuItem copyMenuItem = new JMenuItem("Copy");
    
    private static HtmlView instance;
    
    private HtmlView() {  
    	
    	textArea.setEditable(true);
        copyMenuItem.addActionListener(this);
        
        cutpasteMenu.add(copyMenuItem);
        
        textArea.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                switch(e.getModifiers()) {
                    case InputEvent.BUTTON3_MASK: {
                        cutpasteMenu.show(e.getComponent(), e.getX(), e.getY());
                        break;
                    }
                }
            }
        });
        
        textArea.setEditable(false);
        
        mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
        
        mainPane.add(descrLabel);
        mainPane.add(new JScrollPane(textArea));
        
        this.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width, 150));
        
        setContentPane(mainPane);
        pack();
    }
    
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if (source == copyMenuItem) {
        	JTextArea jte = (JTextArea)cutpasteMenu.getInvoker();
            jte.copy();
        }
    }

	public static HtmlView getInstance() {
		if (instance == null) {
			instance = new HtmlView();
		}
		
		return instance;
	}
    
    public void showText(String text) {
    	textArea.setText(text);
    	pack();
    	setVisible(true);
    }
}